package com.gingko.duominuo.ui.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gingko.duominuo.data.model.Value
import com.gingko.duominuo.databinding.ItemTaskFilterBinding
import com.gingko.duominuo.databinding.ItemWorkOrderRecordBinding
import com.gingko.duominuo.utils.singleClick

class TaskFilterAdapter(val context: Context, private val data: List<Value>) : RecyclerView.Adapter<TaskFilterAdapter.TaskFilterHolder>() {
    var selectPos: Int? = null

    inner class TaskFilterHolder(private val viewBinding: ItemTaskFilterBinding) : RecyclerView.ViewHolder(viewBinding.root) {

        @SuppressLint("SetTextI18n")
        fun bind(filter: Value, position: Int) {
            viewBinding.root.isSelected = selectPos == position
            viewBinding.tvFilter.text = filter.desc
            viewBinding.root.singleClick {
                selectPos = position
                notifyDataSetChanged()
            }
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TaskFilterHolder {
        return TaskFilterHolder(ItemTaskFilterBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder: TaskFilterHolder, position: Int) {
        holder.bind(data[position], position)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    fun getSelectFilter(): Value? {
        return selectPos?.let { data[it] }
    }
}